import time
from Common.Utils import WebDriver
from Test.Login import login
from selenium.webdriver.common.by import By


class Class:
    def __init__(self):
        self.url = "http://localhost:8080/#/login?redirect=%2Fdashboard"
        self.driver = WebDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(3)

    # self.driver.find_element(By.CSS_SELECTOR, '').click()

    # 登录工作
    def Login(self, user, password):
        login.Init(user, password)
        login.autoIdentifyCode()
        time.sleep(7)
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > form > button").click()
        # 点击班级管理页面
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.sidebar-container.has-logo > div.el-scrollbar > div.scrollbar-wrapper.el-scrollbar__wrap > div > ul > div:nth-child(4) > a > li').click()

    # 添加新的班级, 不填写参数
    def AddFail(self):
        self.Login('admin', 'admin')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(1) > div > button').click()
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary > span').click()

        time.sleep(0.5)
        self.driver.save_screenshot('../images/class/class1.png')

    # 正确填写参数添加
    def AddSuc(self, path='../images/class/class2.png'):
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(1) > div > div.el-input > input').send_keys('六年一班')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(2) > div > div.el-input > input').send_keys('20')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(3) > div > div > input').send_keys('aaa')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(4) > div > div.el-input > input').send_keys('aaa@qq.com')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(5) > div > div > input').send_keys('13800000000')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(6) > div > div > div > input').click()
        # 手动选择插入的年级（动态id无法锁定）
        time.sleep(3)

        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(7) > div > div > input').send_keys('aaaaaaaaaa')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot(path)


    # 重复添加, 再次调用AddSuc函数, 使用缺省参数改变图片路径
    def AddFail2(self):
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(1) > div > button').click()
        self.AddSuc('../images/class/class3.png')

    # 从年级开始查询
    def SelectGrade(self):
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(2) > div > div > div > input').click()
        # 手动选择, 规避动态ID问题
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(4) > div > button.el-button.el-button--primary.el-button--small > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/class/class4.png')

    # 重置查询
    def ReSet(self):
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(4) > div > button.el-button.el-button--info.el-button--small > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/class/class5.png')

    # 从班级开始查询
    def SelectClass(self):
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(3) > div > div > input').clear()
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(3) > div > div > input').send_keys('六年一班')
        self.driver.find_element(By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div > div:nth-child(1) > form > div:nth-child(4) > div > button.el-button.el-button--primary.el-button--small > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/class/class6.png')

    # 修改六年一班信息,改为六年二班, 但是空填班级
    def Modify(self):
        time.sleep(7)  # 手动选择六年一班
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(1) > div > div > input').clear()
        time.sleep(5)
        # 手动清空六年一班行

        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/class/class7.png')

        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__body > form > div:nth-child(1) > div > div > input').send_keys(
            '六年二班')
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#app > div > div.main-container > section > div > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary > span').click()
        time.sleep(0.5)
        self.driver.save_screenshot('../images/class/class8.png')



    # 删除(Delete) 需动态手工测试

# Class = Class()
# Class.AddFail()
# Class.AddSuc()
# Class.AddFail2()
# Class.SelectGrade()
# Class.ReSet()
# Class.SelectClass()
# Class.Modify()



# self.driver.find_element(By.CSS_SELECTOR, '').click()